package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

//@Mapper注解; 表示是mybatis中的mapper接口
//程序运行时 框架会自动生成接口的实现类对象 并交给springioc容器管理
@Mapper
public interface UserInfoMapper {
    //查询所有用户
    @Select("select * from userinfo")
    @Results(id = "resultMap", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<UserInfo> queryAllUser();

    //查询指定id的用户
    @Select("select * from userinfo where id = ${id}")
    @ResultMap(value = "resultMap")
    List<UserInfo> queryUserById(Integer id);

    @Select("select * from userinfo where username = '${username}'")
    List<UserInfo> queryUserByName(String username);

    //获取主键注解
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender,phone) " +
            "values(#{username},#{password},#{age},#{gender},#{phone})")
    Integer insert(UserInfo userInfo);


    @Insert("insert into userinfo (username,password,age,gender,phone) " +
            "values(#{userinfo.username},#{userinfo.password},#{userinfo.age},#{userinfo.gender},#{userinfo.phone})")
    Integer insert2(@Param("userinfo") UserInfo userInfo);

    @Delete("delete from userinfo where id = #{id}")
        //根据id删除
    Integer delete(Integer id);

    @Update("update userinfo set username=#{username} where id=#{id}")
    Integer update(UserInfo userInfo);

    @Select("select * from userinfo order by id #{sort}")
    List<UserInfo> queryAllUserBySort(String sort);

    @Select("select * from userinfo where username like concat('%',#{key},'%')")
    List<UserInfo> queryAllUserByLike(String key);
}
